package com.sgb.kjwl.view.ui.activity.manage

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.scwang.smart.refresh.layout.listener.ScrollBoundaryDecider
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityWebsiteAnnouncementListBinding
import com.sgb.kjwl.databinding.ItemAnnouncementListBinding
import com.sgb.kjwl.model.entity.manage.AnnouncementListEntity
import com.sgb.kjwl.viewmodel.manage.WebsiteAnnouncementListViewModel
import com.swgk.core.BaseAppHelper
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration

/**
 * 公告列表
 */
class WebsiteAnnouncementListActivity : AppActivity() {

    private lateinit var mViewModel : WebsiteAnnouncementListViewModel

    private lateinit var mBinding : ActivityWebsiteAnnouncementListBinding

    private var isLoadMore = false //是否加载更多
    private var isEnableLoadMore = false//是否能加载更多

    private lateinit var mAdapter : AnnouncementListAdapter

    private var mSiteNo = ""
    private var mSiteType = ""

    companion object{
        const val KEY_NO = "key_site_no"
        const val KEY_TYPE = "key_site_type"
        fun launch(context: Context, siteNo : String, siteType : String){
            val intent = Intent()
            intent.setClass(context, WebsiteAnnouncementListActivity::class.java)
            intent.putExtra(KEY_NO, siteNo)
            intent.putExtra(KEY_TYPE, siteType)
            context.startActivity(intent)
        }
    }

    override fun init() {
        super.init()
        isLoadMore = false
        mViewModel.getAnnouncementList(isLoadMore, mSiteNo, mSiteType, )
    }


    override fun initView() {
        super.initView()
        if (intent != null){
            mSiteNo = intent.getStringExtra(KEY_NO)!!
            mSiteType = intent.getStringExtra(KEY_TYPE)!!
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_website_announcement_list)
        mViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(BaseAppHelper.get().instance).create(
            WebsiteAnnouncementListViewModel::class.java)
        mBinding.include.titleView.text = "公告列表"
        mBinding.include.ivBack.setOnClickListener { finish() }
        initRecyclerView()
        mViewModel.mWebsiteList.observe(this, {
            isEnableLoadMore = it != null && it.size >= 10
            if (it != null){
                if(isLoadMore){
                    mAdapter.addData(it)
                } else {
                    mAdapter.setNewData(it)
                }
            }
        })
    }

    private fun initRecyclerView() {
        // SwipeRefreshLayout 刷新
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                isLoadMore = true
                mViewModel.getAnnouncementList(isLoadMore, mSiteNo, mSiteType, )
                mBinding.refreshLayout.finishLoadMore(500)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                isLoadMore = false
                mViewModel.getAnnouncementList(isLoadMore, mSiteNo, mSiteType, )
                mBinding.refreshLayout.finishRefresh(500)
            }
        })
        //解决 在实现 上拉加载更多 和 下拉刷新 时  遇到item不在顶端就执行了刷新的方法和数据item没有填满屏幕也执行了加载更多
        mBinding.refreshLayout.setScrollBoundaryDecider(object : ScrollBoundaryDecider {
            override fun canRefresh(content: View): Boolean {
                if (mBinding.recycleView == null) return false
                return mBinding.recycleView.computeVerticalScrollOffset() === 0
            }

            override fun canLoadMore(content: View): Boolean {
                if (mBinding.recycleView == null) return false
                //整个View控件的高度
                val scrollRange: Int = mBinding.recycleView.computeVerticalScrollRange()
                //当前屏幕之前滑过的距离
                val scrollOffset: Int = mBinding.recycleView.computeVerticalScrollOffset()
                //当前屏幕显示的区域高度
                val scrollExtent: Int = mBinding.recycleView.computeVerticalScrollExtent()
                //获取recyclerView的高度
                val height: Int = mBinding.recycleView.height
                if (height > scrollRange) {
                    return false
                }
                return isEnableLoadMore && scrollRange <= scrollOffset + scrollExtent
            }
        })
        mBinding.recycleView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        mBinding.recycleView.setItemViewCacheSize(100)
        mBinding.recycleView.setHasFixedSize(true)
        mAdapter = AnnouncementListAdapter(this, R.layout.item_announcement_list)
        mAdapter.bindToRecyclerView(mBinding.recycleView)
        val empty = LayoutInflater.from(this)
            .inflate(R.layout.layout_empty_view, mBinding.recycleView, false)
        mAdapter.emptyView = empty
        mBinding.recycleView.addItemDecoration(
            RecyclerCustomDecoration(
                0f,
                0f,
                0f,
                resources.getDimension(R.dimen.dp1), resources.getColor(R.color.color_line)
            )
        )
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (mAdapter.data != null && mAdapter.data.isNotEmpty() && mAdapter.data[position] != null){
                mViewModel.getH5Url(this, mAdapter.data[position].no, "announcementDetail")
            }
        }

    }


    class AnnouncementListAdapter(mContext : Context, layoutResId : Int) : BaseQuickAdapter<AnnouncementListEntity, BaseViewHolder>(layoutResId){
        private var mItemBinding : ItemAnnouncementListBinding? = null
        override fun convert(helper: BaseViewHolder?, item: AnnouncementListEntity?) {
            mItemBinding = DataBindingUtil.bind(helper!!.itemView)
            if (mItemBinding != null){
                mItemBinding!!.entity = item
                mItemBinding!!.tvPublishTime.text = mContext.resources.getString(R.string.announcement_publish_time, item!!.time)
                mItemBinding!!.tvType.text = item.typeName
                when (item.type) {
                    1 -> {//商品
                        mItemBinding!!.tvType.setColors(R.color.color_secondary_red_third, R.color.color_secondary_red_four)
                    }
                    2 -> {//招采
                        mItemBinding!!.tvType.setColors(R.color.color_secondary_orange_ninth, R.color.color_secondary_orange_fifth)
                    }
                    3 -> {//集采
                        mItemBinding!!.tvType.setColors(R.color.color_secondary_purple_four, R.color.color_secondary_purple_fifth)
                    }
                    4 -> {//租赁
                        mItemBinding!!.tvType.setColors(R.color.color_secondary_yellow_fifth, R.color.color_secondary_yellow_four)
                    }
                    5 -> {//物流
                        mItemBinding!!.tvType.setColors(R.color.color_third_green_easy, R.color.color_secondary_green_easy_six)
                    }
                    6 -> {//工程
                        mItemBinding!!.tvType.setColors(R.color.color_secondary_blue_four, R.color.color_secondary_blue_third)
                    }
                    7 -> {//企服
                        mItemBinding!!.tvType.setColors(R.color.color_secondary_brown, R.color.color_secondary_brown_third)
                    }
                    8 -> {//商品
                        mItemBinding!!.tvType.setColors(R.color.color_secondary_grey, R.color.color_secondary_grey_third)
                    }
                }
            }
        }

    }

}